--[[
LogFile Creator by grey
0.3
]]


local writemode = "w+" --update mode, all previous data is erased;
--local writemode = "a+" --append update mode, previous data is preserved, writing is only allowed at the end of file.
local function logToFile(name, arguments)
    local t = ""
    if name then
        t = t .. "[" .. os.date("%c") .. ": " .. " ] " .. name .. " ("
        if arguments then
            local i = 0
            for k, v in pairs(arguments) do
                i = i + 1
                if type(k) == "string" then
                    t = t .. k .. ": "
                end
                if not v then local v = "nil" end
                if type(v) == "string" or type(v) == "number" then
                    t = t .. v
                elseif type(v) == "table" or type(v) == "userdata" then
                    if v.name then
                        t = t .. v.name
                    else t = t .. "Unknown Table/Object"
                    end
                else t = t .. "Unknown"
                end
                t = t .. ", "
            end
        end
        t = t .. ") \n"
    end
    local file, error = assert(io.open(SCRIPT_PATH .. "BoL.log", writemode))
    if error then return error end
    file:write(t)
    file:close()
end

local function logfunction()
    local arguments = {}
    local i, k, v = 1, debug.getlocal(2, 1)
    repeat
        if k ~= "(*temporary)" then
            arguments[k] = v
        end
        i = i + 1
        k, v = debug.getlocal(2, i)
        until not k
    local t = ""
    local line = debug.getinfo(2, "l").currentline
    local name = debug.getinfo(2, "n").name
    if line and line >= 0 then t = t .. "Line " .. line .. ": " end
    if name then t = t .. name else t = t .. "Unknown Function" end
    if t ~= "" then logToFile(t, arguments) end
end

debug.sethook(logfunction, "c")